package org.sunshine.miracle.web.gwt.client;

//import com.extjs.gxt.samples.client.Examples;
//import com.extjs.gxt.samples.client.FileServiceAsync;
//import com.extjs.gxt.samples.client.examples.model.FileModel;
//import com.extjs.gxt.samples.client.examples.model.FolderModel;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.binder.TreeBinder;
import com.extjs.gxt.ui.client.data.BaseTreeLoader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelStringProvider;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.data.TreeLoader;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.store.StoreSorter;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.tree.Tree;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

import java.util.List;

/**
 * User: djiva
 * Date: Dec 10, 2008
 * Time: 1:22:39 PM
 */
public class FileTreeContainer extends LayoutContainer {
    @Override
    protected void onRender(Element parent, int pos) {
        super.onRender(parent, pos);

        setLayout(new FlowLayout(10));

        final FileServiceAsync service = FileService.ServiceLoader.getInstance();

        // data proxy
        RpcProxy<FileModel, List<FileModel>> proxy = new RpcProxy<FileModel, List<FileModel>>() {
            @Override
            protected void load(FileModel loadConfig, AsyncCallback<List<FileModel>> callback) {
                service.getFolderChildren(loadConfig, callback);
            }
        };

        // tree loader
        TreeLoader<FileModel> loader = new BaseTreeLoader<FileModel>(proxy) {
            @Override
            public boolean hasChildren(FileModel parent) {
                return parent.isDirectory();
            }
        };

        // trees store
        TreeStore<FileModel> store = new TreeStore<FileModel>(loader);
//        store.setStoreSorter(new StoreSorter<FileModel>() {
//
//            @Override
//            public int compare(Store store, FileModel m1, FileModel m2, String property) {
//                boolean m1Folder = m1 instanceof FolderModel;
//                boolean m2Folder = m2 instanceof FolderModel;
//
//                if (m1Folder && !m2Folder) {
//                    return -1;
//                } else if (!m1Folder && m2Folder) {
//                    return 1;
//                }
//
//                return super.compare(store, m1, m2, property);
//            }
//        });

        Tree tree = new Tree();

        TreeBinder<FileModel> binder = new TreeBinder<FileModel>(tree, store);
//        binder.setIconProvider(new ModelStringProvider<FileModel>() {
//
//            public String getStringValue(FileModel model, String property) {
//                if (!(model instanceof FolderModel)) {
//                    String ext = model.getName().substring(model.getName().lastIndexOf(".") + 1);
//
//                    // new feature, using image paths rather than style names
//                    if ("xml".equals(ext)) {
//                        return "images/icons/page_white_code.png";
//                    } else if ("java".equals(ext)) {
//                        return "images/icons/page_white_cup.png";
//                    } else if ("html".equals(ext)) {
//                        return "images/icons/html.png";
//                    } else {
//                        return "images/icons/page_white.png";
//                    }
//                }
//                return null;
//            }
//
//        });
        binder.setDisplayProperty("name");

        loader.load(null);
        addText("<div style='font-size: 12px;padding-bottom: 8px'>Tree which loads its children on demand using a remote service.</div>");
        add(tree);
    }
}